package com.huisou.search;

import com.huisou.basedataservice.AreaDao;
import com.huisou.basedataservice.AskCategoryDao;
import com.huisou.basedataservice.CategoryDao;
import com.huisou.basedataservice.InfoCategoryDao;
import com.huisou.basedataservice.JobTypeDao;
import com.huisou.basedataservice.MicrocodeDao;
import com.huisou.basedataservice.ProfessionDao;
import com.huisou.utils.DateUtil;
import com.huisou.utils.SpringContextHolder;
import com.huisou.utils.StrUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.tools.generic.FormatConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class B2bTool extends FormatConfig {
	
	public static Logger	log	= LoggerFactory.getLogger(B2bTool.class);
	
	private AreaDao			ad;
	
	private CategoryDao		cd;
	
	private ProfessionDao	professionDao;
	
	private InfoCategoryDao	infoCategoryDao;
	
	private AskCategoryDao	acd;
	
	private JobTypeDao		jd;
	
	private MicrocodeDao	md;
	
	public B2bTool() {
		ad = (AreaDao) SpringContextHolder.getBean("areaDao");
		cd = (CategoryDao) SpringContextHolder.getBean("categoryDao");
		professionDao = (ProfessionDao) SpringContextHolder.getBean("professionDao");
		infoCategoryDao = (InfoCategoryDao) SpringContextHolder.getBean("infoCategoryDao");
		acd = (AskCategoryDao) SpringContextHolder.getBean("askCategoryDao");
		jd = (JobTypeDao) SpringContextHolder.getBean("jobTypeDao");
		md = (MicrocodeDao) SpringContextHolder.getBean("microcodeDao");
	}
	
	/**
	 * 获取城市名称
	 * 
	 * @param id
	 * @return
	 */
	public String areaName(String id) {
		if (StringUtils.isBlank(id)) {
			return "";
		}
		Integer iid = Integer.valueOf(id);
		return ad.getAreaNameById(iid);
	}
	
	/**
	 * 获取代码名称
	 * 
	 * @param code
	 * @return
	 */
	public String codeName(int id) {
		if (id == 0) {
			return "";
		}
		return md.getCodeNameById(id);
	}
	
	/**
	 * 获取公司所在行业类别
	 * 
	 * @param code
	 * @return
	 */
	public String profName(String code) {
		String profName = "";
		try {
			profName = professionDao.getNameByCode(code);
		}
		catch (Exception e) {
			log.error("取profession出错:" + e.getMessage() + "----->code:" + code + ",name:" + profName);
		}
		
		return profName;
	}
	
	/**
	 * 获取产品供应及求购的类别名称
	 * 
	 * @param code
	 * @return
	 */
	public String cateName(String code) {
		String cateName = "";
		if (StringUtils.isNotBlank(code)) {
			try {
				cateName = cd.getCatsNameByCode(code);
			}
			catch (Exception e) {
				log.error("取cateName出错:" + e.getMessage() + "----->code:" + code + ",cateName:" + cateName);
			}
		}
		return cateName;
	}
	
	/**
	 * 获取职位对应的行业类别
	 * 
	 * @param code
	 * @return
	 */
	public String jobTypeName(String id) {
		Integer iid = Integer.valueOf(id);
		return jd.getJobNameById(iid);
	}
	
	/**
	 * 获取资讯类别名称
	 * 
	 * @param code
	 * @return
	 */
	public String infoCateName(String code) {
		String infoCateName = "";
		try {
			infoCateName = infoCategoryDao.getNameByCode(code);
		}
		catch (Exception e) {
			log.error("出infocateName出错----->code:" + code + ",name:" + infoCateName);
		}
		return infoCateName;
	}
	
	/**
	 * 获取生意经类别名称
	 * 
	 * @param code
	 * @return
	 */
	public String askCateName(String code) {
		if (code != null) {
			int id = Integer.parseInt(code);
			return acd.getNameById(id);
		}
		return "";
	}
	
	/**
	 * 获取金牌会员年限，无效则返回0
	 * 
	 * @return
	 */
	public int goldYears(Date start, Date end) {
		// 如果开始时间为null就返回0
		if (start == null) {
			return 0;
		}
		
		// 如果结束时间比现在时间小就返回0
		if (end != null && DateUtil.compareDate(new Date(), end)) {
			return 0;
		}
		
		return DateUtil.getYears(start);
	}
	
	/**
	 * 获取会员等级名称
	 * 
	 * @return
	 */
	public String getGradeName(int grade) {
		String gradeName = null;
		switch (grade) {
			case 1:
				gradeName = "普通会员";
				break;
			case 2:
				gradeName = "初级会员";
				break;
			case 3:
				gradeName = "高级会员";
				break;
			case 4:
				gradeName = "黄金会员";
				break;
			case 5:
				gradeName = "白金会员";
				break;
			case 6:
				gradeName = "钻石会员";
				break;
			case 7:
				gradeName = "双钻石";
				break;
			case 8:
				gradeName = "三钻石";
				break;
			case 9:
				gradeName = "皇冠";
				break;
			case 10:
				gradeName = "双皇冠";
				break;
			case 11:
				gradeName = "三皇冠";
				break;
			case 12:
				gradeName = "发起人";
				break;
			default:
				gradeName = "普通会员";
				break;
		}
		return gradeName;
	}
	
	/**
	 * 获取产品链接
	 */
	public String prodLink(String dm, String comid, int tp, String yr, String pid) {
		String url = dm + "/" + comid;
		if (1 == tp)
			url += "/sale";
		else
			url += "/buy";
		url += "/" + yr + "/" + pid + ".html";
		return url;
	}
	
	/**
	 * 获取资讯链接
	 */
	public String infoLink(String dm, String dt, String iid, String cateCode) {
		String url = dm + "/news";
		if (cateCode != null && cateCode.startsWith("01"))
			url = dm + "/price";
		String[] ds = dt.split("-");
		url += "/" + ds[0] + "/" + Integer.parseInt(ds[1]) + "/" + iid + ".html";
		return url;
	}
	
	/**
	 * 获取企业资讯链接
	 */
	public String comInfoLink(String dm, String dt, String iid, int comId) {
		String url = dm + "/" + comId + "/news";
		url += "/" + dt + "/" + iid + ".html";
		return url;
	}
	
	/**
	 * 获取招聘链接
	 */
	public String comJobLink(String dm, String dt, String iid, int comId) {
		String url = dm + "/" + comId + "/recruit";
		url += "/" + dt + "/" + iid + ".html";
		return url;
	}
	
	/**
	 * 截取字符串
	 * 
	 * @param s
	 * @param len
	 * @return
	 */
	public String htmlCut(String s, int len, String key) {
		if (s != null && s.length() > 0) {
			String str = StrUtils.htmlCut(s, len, "...");
			if (StringUtils.isNotBlank(key)) {
				str = str.replaceAll(key, "<em>" + key + "</em>");
			}
			str = str.replaceAll("&nbsp;", "");
			return str;
		}
		else {
			return "";
		}
	}
	
	/**
	 * 字符串替换
	 * 
	 * @param s
	 * @param len
	 * @return
	 */
	public String replace(String s, String regex, String val) {
		s = s.replaceAll(regex, val);
		return s;
	}
	
	public String encode(String str) {
		try {
			String s = URLEncoder.encode(str, "GBK");
			return s;
		}
		catch (UnsupportedEncodingException e) {
			return str;
		}
	}
	
}
